﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Intelligent.OTC.Domain.DataModel;
using Intelligent.OTC.Business;
using Intelligent.OTC.Common;
using Intelligent.OTC.Domain.DataModel;
using Intelligent.OTC.WebApi.Core;
using Intelligent.OTC.Common.Attr;

namespace Intelligent.OTC.WebApi.Controllers
{
    [UserAuthorizeFilter(actionSet: "common")]
    public class MailTemplateController : ApiController
    {
        [HttpGet]
        [PagingQueryable]
        public IQueryable<MailTemplate> Get()
        {
            IMailService service = SpringFactory.GetObjectImpl<IMailService>("MailService");
            return service.GetMailTemplates().AsQueryable();
        }

        [HttpPut]
        public void Put([FromBody]MailTemplate template)
        {
            IMailService service = SpringFactory.GetObjectImpl<IMailService>("MailService");
            service.SaveOrUpdateTemplate(template);
        }

        [HttpPost]
        public void Post([FromBody]MailTemplate template)
        {

            IMailService service = SpringFactory.GetObjectImpl<IMailService>("MailService");
            service.SaveOrUpdateTemplate(template);
        }

        //[HttpGet]
        //public MailTemplate Get(string type)
        //{
        //    if (type == "new")
        //    {
        //        MailTemplate mailtemp = new MailTemplate();
        //        mailtemp.Id = 0;
        //        return mailtemp;
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}

        [HttpDelete]
        public void delete(int id)
        {
            MailService service = SpringFactory.GetObjectImpl<MailService>("MailService");
            MailTemplate old = service.CommonRep.FindBy<MailTemplate>(id);
            if (old != null)
            {
                service.CommonRep.Remove(old);
                service.CommonRep.Commit();
            }

        }
    }
}